package com.demo.rocketmq.consumer;

import com.demo.rocketmq.annotation.MqConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 测试消息堆积问题
 */
@Slf4j
@Service
public class BlockConsumer {

    public static final String BLOCK_TOPIC = "test_block_topic";

    @MqConsumer(topic = BLOCK_TOPIC, consumerGroup = "test_block_topic_normal")
    public void normal(String message) {
        long value = Long.parseLong(message.split("-")[0].strip());
        if (value % 500 == 0) {
            log.info("正常消费，内容：" + message);
        }
    }

    /**
     * 模拟耗时CPU计算，消息消费缓慢，通过JStack查询线程状况
     * @param message
     * @throws InterruptedException
     */
    @MqConsumer(topic = BLOCK_TOPIC, consumerGroup = "test_block_topic_block")
    public void block(String message) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            log.info("模拟耗时CPU计算，消息消费缓慢，通过JStack查询线程状况，第{}次循环",  i);
            for (long l = 0; l < 2100000000; l++) {
            }
        }
        log.info("阻塞消费，内容：{}", message);
    }
}
